#include <spu_mfcio.h>
#include <simdmath.h>
#include "../ubicus.h"

// DPCM

vector PRECISION qusat(vector PRECISION x)
{
	vector PRECISION retour = x;

#ifdef ENFLOAT
	vector unsigned int masque;
	vector PRECISION masque_plus = {1.0, 1.0, 1.0, 1.0};
	vector PRECISION masque_moins = {-1.0, -1.0, -1.0, -1.0};
	masque = isgreaterf4(retour, masque_plus);
	retour = spu_sel(retour, masque_plus, masque);
	masque = islessf4(retour, masque_moins);
	retour = spu_sel(retour, masque_moins, masque);
#else
	vector unsigned long long masque;
	vector PRECISION masque_plus = {1.0, 1.0};
	vector PRECISION masque_moins = {-1.0, -1.0};
	masque = isgreaterd2(retour, masque_plus);
	retour = spu_sel(retour, masque_plus, masque);
	masque = islessd2(retour, masque_moins);
	retour = spu_sel(retour, masque_moins, masque);
#endif
	
	
	
	return retour;
}

void recu(vector PRECISION * x,  vector PRECISION * y, vector PRECISION a, vector PRECISION b)
{
#ifdef ENFLOAT
	vector PRECISION p = {5.0, 5.0, 5.0, 5.0};
	vector PRECISION s = {0.5, 0.5, 0.5, 0.5};
#else
	vector PRECISION p = {5.0, 5.0};
	vector PRECISION s = {0.5, 0.5};
#endif
	vector PRECISION tmpx;
	tmpx = *x;
	*x = *y;
	*y = spu_add(spu_add(spu_add(spu_mul(a, *y), spu_mul(b, tmpx)),	spu_mul(a, qusat(spu_mul(p, spu_sub(s, *y))))), spu_mul(b, qusat(spu_mul(p, spu_sub(s, tmpx)))));
	

}

